﻿using System;
using System.Data;

namespace BLL
{
    public class OracleHelper
    {
        /// <summary>
        ///  获得该SQL查询返回DataTable，如果没有查询到则返回NULL
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            DataTable tb = null;
            tb = Utility.DbHelperSQL.GetDataTable(sql);
            //DataSet ds = this.GetDataSet(sql);
            //if (ds != null)
            //{
            //    tb = ds.Tables["tempTable"];
            //}
            return tb;
        }

        /// <summary>
        /// 对数据库的增，删，改的操作带事务
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>是否成功</returns>
        public int ExecuteDataBase(string sql, string OperaName)
        {
            int cnt = 0;
            try
            {
                cnt = Utility.DbHelperSQL.ExecuteSqlLog(sql, OperaName);
            }
            catch (Exception)
            {
                throw;
            }

            #region 错误

            //OracleTransaction tx = null;
            //try
            //{
            //    conn = DataBase();
            //    this.Open();
            //    tx = conn.BeginTransaction();
            //    OracleCommand cmd = new OracleCommand();
            //    cmd.Connection = conn;
            //    cmd.CommandText = sql;

            //    cmd.Transaction = tx;
            //    cnt = cmd.ExecuteNonQuery();
            //    tx.Commit();
            //}
            //catch (Exception e)
            //{
            //    tx.Rollback();
            //    throw e;
            //}
            //finally
            //{
            //    this.Close();
            //    this.Dispose();
            //}
            //return cnt;

            //try
            //{
            //    //写文本日志
            //    writeLog(sql, OperaName);
            //}
            //catch (Exception e)
            //{
            //    //写错误日志
            //    StringBuilder xsb = new StringBuilder();
            //    xsb.Append("<?xml version=\"1.0\"?>\r\n");
            //    xsb.Append("<root>\r\n");
            //    xsb.Append("<ErrorInfo>" + e.Message + "</ErrorInfo>\r\n");
            //    xsb.Append("<ExcuteSQL>" + sql + "</ExcuteSQL>\r\n");
            //    xsb.Append("<DataFor>sql</DataFor>\r\n");
            //    xsb.Append("</root>\r\n");

            //    CreateErrorDOC(xsb.ToString());
            //    tx.Rollback();
            //    throw e;
            //}
            //finally
            //{
            //    this.Close();
            //    this.Dispose();
            //}

            #endregion 错误

            return cnt;
        }

        /// <summary>
        /// 增 删 改 复合语句 带事务
        /// </summary>
        /// <param name="sql">以;间隔的SQL语句</param>
        /// <returns>1 成功 0 失败</returns>
        public int ExecuteDataBaseEx(string sql)
        {
            int cnt = 0;
            //OracleTransaction tx = null;
            //try
            //{
            //    conn = DataBase();
            //    this.Open();
            //    tx = conn.BeginTransaction();
            //    OracleCommand cmd = new OracleCommand();
            //    cmd.Connection = conn;
            //    cmd.Transaction = tx;

            //    if (sql.Contains(";"))
            //    {
            //        string[] sqls = sql.Split(';');

            //        for (int i = 0; i < sqls.Length; i++)
            //        {
            //            cmd.CommandText = sqls[i];
            //            cnt += cmd.ExecuteNonQuery();
            //        }
            //        tx.Commit();
            //        cnt = 1;

            //    }
            //    else
            //    {
            //        cmd.CommandText = sql;
            //        cnt = cmd.ExecuteNonQuery();
            //        tx.Commit();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    string exMessage = ex.Message;
            //    tx.Rollback();
            //    cnt = 0;
            //    throw ex;
            //}
            //finally
            //{
            //    this.Close();
            //    this.Dispose();
            //}

            return cnt;
        }
    }
}